﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace MII_FIF.Layouts.MII_FIF.ChangeRequest
{
    public partial class CRVMWare : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.ChangeRequestGroupName))
            {
                btnSubmit.Click += btnSubmit_Click;
                btnCancel.Click += btnCancel_Click;
                DDLLabelName.SelectedIndexChanged += DDLLabelName_SelectedIndexChanged;
                ((Label)Header.FindControl("lblHeader")).Text = "Change Request VM Ware";
                if (!IsPostBack)
                {
                    load();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/ChangeRequest/ViewCRVMWare.aspx"));
        }

        void DDLLabelName_SelectedIndexChanged(object sender, EventArgs e)
        {
            InventoryManagement.Inventory_VM VM = new InventoryManagement.Inventory_VM();
            InventoryManagement.Location Location = new InventoryManagement.Location();
            InventoryManagement.Rack_Number rack = new InventoryManagement.Rack_Number();

            VM.ID = Guid.Parse(DDLLabelName.SelectedItem.Value);
            VM.Select();

            rack.ID = VM.ID_RackNumber;
            rack.Select();
            Location.ID = VM.ID_DataCabang;
            Location.Select();
            txtLocation.Text = Location.Lokasi;
            txtIP.Text = VM.Host_IP;
            txtHostName.Text = VM.Host_Name;
            txtRackNumber.Text = rack.Title;
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            if (validasi())
            {
                string URLUpload = Generic.UploadFile(FUAttachment);
                string SystemClass = "";
                #region systemclass
                if (rbBladeServer.Checked)
                    SystemClass = rbBladeServer.Text;

                if (rbMainframe.Checked)
                    SystemClass = rbMainframe.Text;

                if (rbRackMountServer.Checked)
                    SystemClass = rbRackMountServer.Text;

                if (rbStorage.Checked)
                    SystemClass = rbStorage.Text;

                if (rbTapeStorage.Checked)
                    SystemClass = rbTapeStorage.Text;

                if (rbTowerServer.Checked)
                    SystemClass = rbTowerServer.Text;

                if (rbVMWare.Checked)
                    SystemClass = rbVMWare.Text;
                #endregion
                #region CR
                InventoryManagement.CR_VM CR = new InventoryManagement.CR_VM();

                CR.ID = Guid.NewGuid();
                CR.LabelName = DDLLabelName.SelectedItem.Text;
                CR.wfStatus = word.WaitingforApproval;
                CR.Date = DateTime.Now;
                CR.Configuration = ((TextBox)GridChange.Rows[0].FindControl("txtIsi")).Text;
                CR.CR_Interface = chkInterface.Checked;
                CR.CR_LogicalConnection = chkLogicalConnection.Checked;
                CR.CR_System = chkSystem.Checked;
                CR.CR_VLAN = chkVLAN.Checked;
                CR.Location = txtLocation.Text;
                CR.RackNumber = txtRackNumber.Text;
                CR.InventoryBy = lblInventoryBy.Text;
                CR.IP = txtIP.Text;
                CR.SystemClass = SystemClass;
                CR.HostName = txtHostName.Text;
                CR.ID_VM = Guid.Parse(DDLLabelName.SelectedItem.Value);
                CR.URLAttachment = URLUpload;
                CR.Insert();
                #endregion
                #region apps
                InventoryManagement.Approval apps = new InventoryManagement.Approval();
                apps.ID = Guid.NewGuid();
                apps.IDChild = CR.ID;
                apps.Modul = "Change Request VM";
                apps.RequestDate = DateTime.Now;
                apps.Requester = Generic.GetUserLoginReformat();
                apps.URL = "/_layouts/MII_FIF/FormApproval/Approval_CRNetwork.aspx";
                apps.Insert();
                #endregion

                btnCancel_Click(sender, e);
            }
        }

        void load()
        {
            DataTable DTCR = new DataTable();
            DTCR.Columns.Add("Isi", typeof(string));
            DataRow DR = DTCR.NewRow();
            DR["Isi"] = string.Empty;
            DTCR.Rows.Add(DR);
            ViewState["DTCR"] = DTCR;
            GridChange.DataSource = DTCR;
            GridChange.DataBind();
            if (Request.QueryString["ID"] == null)
            {
                InventoryManagement.Inventory_VM VM = new InventoryManagement.Inventory_VM();
                VM.IsDeleted = false;
                DataTable DT = VM.SelectByParam();
                DT.Columns.Add("PhyName", typeof(string));
                if (DT.Rows.Count > 0)
                {
                    foreach (DataRow dr in DT.Rows)
                    {
                        InventoryManagement.Inventory_Physical phy = new InventoryManagement.Inventory_Physical();
                        phy.IsDeleted = false;
                        phy.ID = Guid.Parse(dr["ID_LabelName"].ToString());
                        phy.Select();
                        dr["PhyName"] = phy.LabelName;
                    }
                }

                Generic.BindDDL(DDLLabelName, DT, "PhyName", "ID");


                disable();
                lblDate.Text = DateTime.Now.ToString("dd MMM yyyy");
                lblInventoryBy.Text = SPContext.Current.Web.CurrentUser.Name;
                download.Visible = false;
            }
            else
            {
                FUAttachment.Visible = false;
                download.Visible = true;
                btnSubmit.Visible = false;
                ViewState["ID"] = Request.QueryString["ID"].ToString();
                InventoryManagement.Inventory_VM VM = new InventoryManagement.Inventory_VM();


                DataTable DT = VM.SelectByParam();
                DT.Columns.Add("PhyName", typeof(string));
                if (DT.Rows.Count > 0)
                {
                    foreach (DataRow dr in DT.Rows)
                    {
                        InventoryManagement.Inventory_Physical phy = new InventoryManagement.Inventory_Physical();
                        phy.ID = Guid.Parse(dr["ID_LabelName"].ToString());
                        phy.Select();
                        dr["PhyName"] = phy.LabelName;
                    }
                }

                Generic.BindDDL(DDLLabelName, DT, "PhyName", "ID");



                InventoryManagement.CR_VM CR = new InventoryManagement.CR_VM();
                CR.ID = Guid.Parse(ViewState["ID"].ToString());
                CR.Select();

                #region DDL
                InventoryManagement.Inventory_VM NetworkDDL = new InventoryManagement.Inventory_VM();
                NetworkDDL.ID = CR.ID_VM;
                NetworkDDL.IsDeleted = false;
                if (NetworkDDL.SelectByParam().Rows.Count > 0)
                {
                    DDLLabelName.SelectedIndex = DDLLabelName.Items.IndexOf(DDLLabelName.Items.FindByValue(CR.ID_VM.ToString()));
                }
                else
                {
                    DDLLabelName.Items.Insert(0, CR.LabelName); DDLLabelName.SelectedIndex = 0;
                }
                //if (DDLLabelName.Items.IndexOf(DDLLabelName.Items.FindByValue(CR.ID_Network.ToString())) < 0)
                //{
                //    DDLLabelName.Items.Insert(1, CR.LabelName); DDLLabelName.SelectedIndex = 1;
                //}
                //else
                //{
                //    DDLLabelName.SelectedIndex = DDLLabelName.Items.IndexOf(DDLLabelName.Items.FindByValue(CR.ID_Network.ToString()));
                //}
                #endregion


                //if (DDLLabelName.Items.IndexOf(DDLLabelName.Items.FindByValue(CR.ID_VM.ToString())) < 0)
                //{
                //    DDLLabelName.Items.Insert(1, CR.LabelName); DDLLabelName.SelectedIndex = 1;
                //}
                //else
                //{
                //    DDLLabelName.SelectedIndex = DDLLabelName.Items.IndexOf(DDLLabelName.Items.FindByValue(CR.ID_VM.ToString()));
                //}

                txtHostName.Text = CR.HostName;
                txtIP.Text = CR.IP;
                txtLocation.Text = CR.Location;
                txtRackNumber.Text = CR.RackNumber;
                lblInventoryBy.Text = CR.InventoryBy;
                lblDate.Text = CR.Date.Value.ToString("dd MMM yyyy");
                chkInterface.Checked = CR.CR_Interface.Value;
                chkLogicalConnection.Checked = CR.CR_LogicalConnection.Value;
                chkSystem.Checked = CR.CR_System.Value;
                chkVLAN.Checked = CR.CR_VLAN.Value;
                ((TextBox)GridChange.Rows[0].FindControl("txtIsi")).Text = CR.Configuration;
                ((TextBox)GridChange.Rows[0].FindControl("txtIsi")).Attributes.Add("readonly", "true");
                ViewState["Link"] = CR.URLAttachment;
                if (ViewState["Link"] != null)
                    lblFileName.Text = CR.URLAttachment.Substring(CR.URLAttachment.LastIndexOf("/") + 1, CR.URLAttachment.Length - CR.URLAttachment.LastIndexOf("/") - 1);

                btnCancel.Text = "Close";
                disable();
                DDLLabelName.Enabled = false;
                chkVLAN.Enabled = false;
                chkSystem.Enabled = false;
                chkLogicalConnection.Enabled = false;
                chkInterface.Enabled = false;
                if (rbBladeServer.Text.Trim().ToLower() == CR.SystemClass.Trim().ToLower()) rbBladeServer.Checked = true;
                else if (rbMainframe.Text.Trim().ToLower() == CR.SystemClass.Trim().ToLower()) rbMainframe.Checked = true;
                else if (rbRackMountServer.Text.Trim().ToLower() == CR.SystemClass.Trim().ToLower()) rbRackMountServer.Checked = true;
                else if (rbStorage.Text.Trim().ToLower() == CR.SystemClass.Trim().ToLower()) rbStorage.Checked = true;
                else if (rbTapeStorage.Text.Trim().ToLower() == CR.SystemClass.Trim().ToLower()) rbTapeStorage.Checked = true;
                else if (rbTowerServer.Text.Trim().ToLower() == CR.SystemClass.Trim().ToLower()) rbTowerServer.Checked = true;
                else rbVMWare.Checked = true;
            }
        }

        bool validasi()
        {
            bool check = true;
            if (FUAttachment.FileContent.Length <= 0)
            {
                lblerror.Text += "Please upload attachment. <br />"; check = false;
            }
            else if (Generic.checkUploadedFileName(FUAttachment.FileName))
            {
                lblerror.Text += "Uploaded file name cannot contains # % & * : < > ? /  { | } <br />"; check = false;
            }
            return check;
        }

        void disable()
        {
            rbBladeServer.Enabled = false;
            rbMainframe.Enabled = false;
            rbRackMountServer.Enabled = false;
            rbStorage.Enabled = false;
            rbTapeStorage.Enabled = false;
            rbTowerServer.Enabled = false;
            rbVMWare.Enabled = false;
            txtIP.Attributes.Add("readonly", "true");
            txtHostName.Attributes.Add("readonly", "true");
            txtLocation.Attributes.Add("readonly", "true");
        }
        protected override void OnPreRender(EventArgs e)
        {
            if (ViewState["Link"] != null)
            {
                string script =
                               "function download() {" +
                               "var link ='" + ViewState["Link"].ToString() + "';" +
                               "var retvalue = window.open('" + Generic.GenLink("/_layouts/MII_FIF/Download.aspx") + "?links=' + link"
                             + ", '', 'width=1000,height=1000');}";

                Page.ClientScript.RegisterClientScriptBlock(typeof(System.Web.UI.Page), "opendownload", script, true);
            }
        }

    }
}
